package com.jpetrak.gate.stringid;

import java.net.URL;

/**
 * Interface for all implementations of a StringIdMap. This is a datastructure that
 * maps strings to numbers and the numbers back to the original string. Also, the 
 * datastructure must be persistent in some way. 
 * For persistence, the implementation must be able to store data to a directory and
 * also load data from a directory. Specifying a directory allows for implementation that
 * either use a single file or several files to store the data.
 * The minimum requirement is that the method load(dirURL) initializes the data and 
 * save(dirURL) saves the data but the method commit() may be used to do intermediate 
 * saves (the implementation decides if commit does anything at all).
 * Each implementation should provide a main method that allows to convert a plain text
 * file that contains pattern numbers, pattern ids or pattern words back to the string
 * representation.
 * 
 * @author Johann Petrak
 *
 */
public abstract class StringIdMap {
  
  /**
   * Initialize the implementation and tell it where to store its backing data.
   * 
   */
  public abstract void init(URL dirURL);
  /**
   * Load all data from the file or files in the given directory. How many files are used
   * and what their names are is dependent on the implementation.
   * If load is not called, any data already present in the directory will be discarded when
   * the data is saved.
   * 
   * @param dirURL
   */
  
  /**
   * Clear any data in memory and on disk. 
   */
  public abstract void clear();
  
  public abstract void load();
  /**
   * Save all data to the file or files in the given directory. How many files are used
   * and what their names are is dependent on the implementation.
   * 
   * @param dirURL
   */
  public abstract void save();
  /**
   * Save any non-saved data to the backing disk store. This may be a no-op for implementations
   * where saving can only be done for all data via the save(URL) method.
   */
  public abstract void commit();
  
  public abstract void saveLists();
  
  public abstract int add(String str);
  
  public abstract String get(int idint);
  
  public abstract int get(String str);
  
  public static String intAsId (int nr) {
    return "ID"+nr;
  }
  
  public static String intAsString (int nr) {
    return ""+nr;
  }
  
  public static int StringAsInt(String str) {
    return Integer.parseInt(str);
  }
  
  public static int idAsInt(String id) {
    return Integer.parseInt(id.substring(2));
  }
  
}
